[ Home | Prev | Next ]

Chapter 5: continued


Integrate Multiple Applications

The most dramatic benefits of scripting come with integrating multiple applications. Instead of every product trying to do everything, each product can be focussed on a specific task -- and "wired up" to the outside world with System 7 Apple events. Off-the-shelf applications become "toolkits of solved problems," rich sets of features waiting to be used. Frontier gives script writers the power to assemble the pieces with unprecedented ease, to orchestrate diverse products into sophisticated solutions.

This section outlines a very simple example, reading data from UserLand's uBASE database application and charting it with BarChart. The task is hardly earth-shaking but we hope it will start you thinking about what you can accomplish with the many scriptable applications that are now available.

A "States and Capitals" database comes with uBASE; we'll use it for this example. If the database had the population of each state or some other interesting numerical data, it would make sense to chart that. It doesn't, so we will have to content ourselves with something considerably less interesting. One number that is quite easy to obtain is the number of characters in the state name.

Let's build the script in stages. As we learned earlier, new scripts generally go in the People table: type Cmd-J, enter "people.ME" (substituting your initials for "ME") in the dialog, click OK. Select "New Script" from the "Table" menu, type "chartStates", click OK. Frontier automatically creates an "on" handler with the name of the script so that we can call it from anywhere in the Object Database. We also want to run (and possibly debug) the script directly from the script window, so we should add a line to call this "handler". You can either type the name or copy/paste; the script should then look like:

on chartStates ()
   
chartStates ()
We already know something about BarChart, so we'll start with that. Let's have the script take a single parameter, the title title of the BarChart window. Add "windowTitle" in the "on" line as the name of the parameter (or implicit local variable that will store the value that is passed as the parameter; depending on how you want to think about it). Let's also fill in a value for the parameter so that we don't forget, perhaps "Characters in State Names."
on chartStates (windowTitle)
   
chartStates ("Characters in State Names")
First the script should launch BarChart and create a new window. Let's think ahead a bit: what if we run the script more than once? Since a window with the specified title will exist, BarChart is likely to complain. A more robust solution is to select the window if it already exists (and set the bar count back to zero), otherwise create a new window. Finally, we may want to bring BarChart to the front so we can watch it create the bars. All this detail really has nothing to do with the main purpose of our script; let's put it in a "bundle" structure so that we can collapse it out of the way. Here's the script so far, including some extra comments:
on chartStates (windowTitle)
   « get information from a scriptable database
   « chart it in a scriptable graphics program
   bundle « setup BarChart
      « BarChart uses many of Frontier's "app" verbs
      app.start ("BarChart") « modern apps use the form BarChart.launch ()
      if app.selectWindow (windowTitle) « in case we run this script again
         BarChart.setBarCount (0) « reset
      else
         app.newWindow (windowTitle)
      sys.bringAppToFront ("BarChart") « modern form BarChart.bringToFront()
chartStates ("Characters in State Names")
Click "Compile" if you want to just check the syntax. If you get an error, click "GoTo" and compare that line (or possibly the previous line) to the script shown above. Click "Run". The script isn't very interesting at this point, but it works. Click "Run" again; it also works when the window already exists. Now would be an excellent time to save your Frontier.root file (and/or export just this script).

The script should launch uBASE, open the database file and get the name of the first state (in whatever order the states are stored, which may not be alphabetical). We will need local variable for the unique ID of the database and the state name. To open the database file, we need the path on disk. Assuming that it is still in the default location: inside the uBASE folder of the UserLand Utilities folder of your Frontier folder, we can take advantage of a Frontier "constant" (value) in the Object Database that stores the location of the Frontier folder. Collapse the previous bundle (dbl-click on the item marker) then add the new lines to your script (after the bundle).

on chartStates (windowTitle)
   « get information from a scriptable database
   « chart it in a scriptable graphics program
   bundle « setup BarChart
   local
      path = Frontier.pathstring + "UserLand Utilities:uBASE:"
      ID
      stateName
      i
   bundle « setup uBASE
      uBASE.launch ()
      ID = uBASE.openFile (path + "States Database")
      stateName = uBASE.getFirstRecord (ID) « the state is the "key" field
chartStates ("Characters in State Names")
Note that the local includes "i", a counter variable for the "for" loop that we will add in a moment. Try running the script again. It still isn't very interesting, but it's nice to get small bits working instead of waiting until the end to look for problems.

With all the setup behind us, we can now create the interesting part of the script. Let's loop through the states and add to the chart a new bar with the name of the state and the number of characters the name contains. 50 states may not fit on the chart, let's do 10. The script should close the database file, then it's done. Here's the new code, with the local and both bundles collapsed. (Isn't the outliner great!)

on chartStates (windowTitle)
   « get information from a scriptable database
   « chart it in a scriptable graphics program
   bundle « setup BarChart
   local
   bundle « setup uBASE
   for i = 1 to 10 « just an example, no need to chart all 50
      BarChart.addBar (stateName, string.length (stateName))
      stateName = uBASE.getNextRecord (ID, stateName) « empty if last record
   uBASE.closeFile (ID)
chartStates ("Characters in State Names")
Why did we get the first record from uBASE before the loop and get the "next" record in the loop? Most database programs give you the option of retrieving any record by number, uBASE is small and fast but does not implement this feature. The script is now complete.

Ready? Click "Run" then sit back, relax and watch Frontier do your bidding.

Contents Page | Previous Section | Next Section -- Frontier Install Scripts
HTML formatting by Steven Noreyko January 1996, User Guide revised by UserLand June 1996